package handler

import (
	"strconv"

	"fitness/go-admin/internal/model"
	"fitness/go-admin/internal/service"
	"fitness/go-admin/pkg/response"

	"github.com/gin-gonic/gin"
)

// FileHandler 文件处理器
type FileHandler struct {
	fileService service.FileService
}

// NewFileHandler 创建文件处理器实例
func NewFileHandler(fileService service.FileService) *FileHandler {
	return &FileHandler{
		fileService: fileService,
	}
}

// ======================== 文件上传 ========================

// UploadFile 上传文件
func (h *FileHandler) UploadFile(c *gin.Context) {
	var req model.UploadFileReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误: "+err.Error())
		return
	}

	fileHeader, err := c.FormFile("file")
	if err != nil {
		response.Error(c, 400, "请选择要上传的文件")
		return
	}

	resp, err := h.fileService.UploadFile(fileHeader, &req)
	if err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, resp, "上传成功")
}

// ======================== 文件管理 ========================

// GetFileInfo 获取文件信息
func (h *FileHandler) GetFileInfo(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, 400, "无效的文件ID")
		return
	}

	file, err := h.fileService.GetFileByID(uint(id))
	if err != nil {
		response.Error(c, 404, "文件不存在")
		return
	}

	response.Success(c, file, "获取成功")
}

// GetFileList 获取文件列表
func (h *FileHandler) GetFileList(c *gin.Context) {
	var req model.FileListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, 400, "参数错误: "+err.Error())
		return
	}

	resp, err := h.fileService.GetFileList(&req)
	if err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, resp, "获取成功")
}

// DeleteFile 删除文件
func (h *FileHandler) DeleteFile(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, 400, "无效的文件ID")
		return
	}

	operatorID := uint(1) // 实际项目中从token中获取

	if err := h.fileService.DeleteFile(uint(id), operatorID); err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, nil, "删除成功")
}

// BatchDeleteFiles 批量删除文件
func (h *FileHandler) BatchDeleteFiles(c *gin.Context) {
	var ids []uint
	if err := c.ShouldBindJSON(&ids); err != nil {
		response.Error(c, 400, "参数错误: "+err.Error())
		return
	}

	if len(ids) == 0 {
		response.Error(c, 400, "请选择要删除的文件")
		return
	}

	operatorID := uint(1)

	if err := h.fileService.BatchDeleteFiles(ids, operatorID); err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, nil, "批量删除成功")
}

// GetFileStatistics 获取文件统计
func (h *FileHandler) GetFileStatistics(c *gin.Context) {
	uploaderID, _ := strconv.ParseUint(c.Query("uploaderId"), 10, 32)
	uploaderType := c.Query("uploaderType")

	stats, err := h.fileService.GetFileStatistics(uint(uploaderID), uploaderType)
	if err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, stats, "获取成功")
}

// ======================== 训练照片 ========================

// AddTrainingPhoto 添加训练照片
func (h *FileHandler) AddTrainingPhoto(c *gin.Context) {
	var req model.AddTrainingPhotoReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, 400, "参数错误: "+err.Error())
		return
	}

	if err := h.fileService.AddTrainingPhoto(&req); err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, nil, "添加成功")
}

// GetTrainingPhoto 获取训练照片详情
func (h *FileHandler) GetTrainingPhoto(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, 400, "无效的照片ID")
		return
	}

	photo, err := h.fileService.GetTrainingPhoto(uint(id))
	if err != nil {
		response.Error(c, 404, "照片不存在")
		return
	}

	response.Success(c, photo, "获取成功")
}

// GetTrainingPhotoList 获取训练照片列表
func (h *FileHandler) GetTrainingPhotoList(c *gin.Context) {
	var req model.TrainingPhotoListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, 400, "参数错误: "+err.Error())
		return
	}

	resp, err := h.fileService.GetTrainingPhotoList(&req)
	if err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, resp, "获取成功")
}

// DeleteTrainingPhoto 删除训练照片
func (h *FileHandler) DeleteTrainingPhoto(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, 400, "无效的照片ID")
		return
	}

	memberID := uint(1)

	if err := h.fileService.DeleteTrainingPhoto(uint(id), memberID); err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, nil, "删除成功")
}

// GetMemberPhotoTimeline 获取会员照片时间线
func (h *FileHandler) GetMemberPhotoTimeline(c *gin.Context) {
	memberID, err := strconv.ParseUint(c.Param("memberId"), 10, 32)
	if err != nil {
		response.Error(c, 400, "无效的会员ID")
		return
	}

	photos, err := h.fileService.GetMemberPhotoTimeline(uint(memberID))
	if err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, photos, "获取成功")
}

// TogglePhotoPublic 切换照片公开状态
func (h *FileHandler) TogglePhotoPublic(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, 400, "无效的照片ID")
		return
	}

	var req struct {
		IsPublic bool `json:"isPublic" binding:"required"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, 400, "参数错误: "+err.Error())
		return
	}

	memberID := uint(1)

	if err := h.fileService.TogglePhotoPublic(uint(id), memberID, req.IsPublic); err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, nil, "修改成功")
}

// ======================== 教练证书 ========================

// AddCoachCertificate 添加教练证书
func (h *FileHandler) AddCoachCertificate(c *gin.Context) {
	var req model.AddCertificateReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, 400, "参数错误: "+err.Error())
		return
	}

	if err := h.fileService.AddCoachCertificate(&req); err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, nil, "添加成功")
}

// GetCoachCertificate 获取证书详情
func (h *FileHandler) GetCoachCertificate(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, 400, "无效的证书ID")
		return
	}

	cert, err := h.fileService.GetCoachCertificate(uint(id))
	if err != nil {
		response.Error(c, 404, "证书不存在")
		return
	}

	response.Success(c, cert, "获取成功")
}

// GetCoachCertificateList 获取证书列表
func (h *FileHandler) GetCoachCertificateList(c *gin.Context) {
	var req model.CertificateListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		response.Error(c, 400, "参数错误: "+err.Error())
		return
	}

	resp, err := h.fileService.GetCoachCertificateList(&req)
	if err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, resp, "获取成功")
}

// DeleteCoachCertificate 删除证书
func (h *FileHandler) DeleteCoachCertificate(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, 400, "无效的证书ID")
		return
	}

	coachID := uint(1)

	if err := h.fileService.DeleteCoachCertificate(uint(id), coachID); err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, nil, "删除成功")
}

// VerifyCertificate 验证证书
func (h *FileHandler) VerifyCertificate(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		response.Error(c, 400, "无效的证书ID")
		return
	}

	var req model.VerifyCertificateReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, 400, "参数错误: "+err.Error())
		return
	}

	operatorID := uint(1)

	if err := h.fileService.VerifyCertificate(uint(id), &req, operatorID); err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, nil, "验证成功")
}

// GetCoachCertificates 获取教练的所有证书
func (h *FileHandler) GetCoachCertificates(c *gin.Context) {
	coachID, err := strconv.ParseUint(c.Param("coachId"), 10, 32)
	if err != nil {
		response.Error(c, 400, "无效的教练ID")
		return
	}

	certs, err := h.fileService.GetCoachCertificates(uint(coachID))
	if err != nil {
		response.Error(c, 500, err.Error())
		return
	}

	response.Success(c, certs, "获取成功")
}
